<?php


class Banner
{
    function __construct($db,$req)
    {
        $this->db = $db;
        $this->req = $req;
    }
    function json($sta,$data){
        return json_encode(array('status'=>$sta,'msg'=>$data));
    }
    function add(){
        date_default_timezone_set('prc');
        $name = $this->req['name'];
        $con = $this->req['con'];
        $fcon = $this->req['fcon'];
        $file = $_FILES['file'];
        $t = time();
        $time = date('Y-m-d H:i:s',time());
        $extname = explode('.',$file['name'])[1];
        $_temp = $file['tmp_name'];
        move_uploaded_file($_temp,'./static/' . $t . '.' . $extname);
        $path = '/boke/static/' . $t . '.' . $extname;
        $this->db->query('insert into banner(name,con,time,pjname,fcon,sta) values (?,?,?,?,?,0)',$name,$con,$time,$path,$fcon);
        echo $this->json(200,'上传成功');
    }

    function find(){
        @ $page = (int)$this->req['page'];
        @ $pagesize = (int)$this->req['pagesize'];
        if($page<1){
            $page = 1;
        }
        $start = ($page-1)*$pagesize;
        $r = $this->db->query("select * from banner order by id desc limit {$start},{$pagesize}");
        $all = $this->db->query("select * from banner");
        echo json_encode(array('status'=>200,'msg'=>$r,'total'=>count($all)));
    }
    function des(){
        @ $id = (int)$this->req['id'];
        $r = $this->db->query("select * from banner where id=?",$id);
        $count = (int)$r[0]['see']+1;
        $this->db->query("update banner set see=? where id=?",$count,$id);
        echo json_encode(array('status'=>200,'msg'=>$r));
    }
    function showBanner(){
        $r = $this->db->query("select * from banner where sta=1");
        echo json_encode(array('status'=>200,'msg'=>$r));
    }


    function del(){
        @ $id = $this->req['id'];
        if(empty($id)){
            exit($this->json(500,'必须传入id'));
        }else{
            $this->db->query('delete from banner where id=?',$id);
            echo $this->json(200,'删除成功');
        }
    }
    function edit(){
        date_default_timezone_set('prc');
        $name = $this->req['name'];
        $con = $this->req['con'];
        $fcon = $this->req['fcon'];
        @ $file = $_FILES['file'];
        $id = $this->req['id'];
        $time = date('Y-m-d H:i:s',time());
        if(empty($file)){
            $pjname = $this->req['pjname'];
        }else{
            $t = time();
            $extname = explode('.',$file['name'])[1];
            $_temp = $file['tmp_name'];
            move_uploaded_file($_temp,'./static/' . $t . '.' . $extname);
            $pjname = '/boke/static/' . $t . '.' . $extname;
        }
        $this->db->query('update banner set name=?,con=?,time=?,pjname=?,fcon=? where id=?',$name,$con,$time,$pjname,$fcon,$id);
        echo $this->json(200,'编辑成功');
    }
    function effect(){
        $sta = $this->req['sta'];
        $id = $this->req['id'];
        $this->db->query('update banner set sta=? where id=?',$sta,$id);
        echo $this->json(200,'改变状态成功');
    }
}